package com.atwulidun.linkedlist.testSingleLinkedList;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");
        HeroNode2 hero5 = new HeroNode2(5, "秦明", "霹雳火");
        HeroNode2 hero6 = new HeroNode2(6, "柴进", "小旋风");

        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.addByOrder(hero1);
        doubleLinkedList.addByOrder(hero3);
        doubleLinkedList.addByOrder(hero5);
        doubleLinkedList.addByOrder(hero2);
        doubleLinkedList.addByOrder(hero4);
        doubleLinkedList.addByOrder(hero6);
        doubleLinkedList.list();
        doubleLinkedList.delete(6);
        doubleLinkedList.list();
    }
}
class DoubleLinkedList {
    private HeroNode2 head = new HeroNode2(Integer.MAX_VALUE, null, null);

    public HeroNode2 getHead() {
        return head;
    }

    // 不按顺序添加
    public void add(HeroNode2 node) {
        HeroNode2 temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = node;
        node.pre = temp;
    }

    // 按顺序添加
    public void addByOrder(HeroNode2 node) {
        HeroNode2 temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (node.no > temp.next.no) {
                temp = temp.next;
            } else {
                break;
            }
        }
        node.pre = temp;
        node.next = temp.next;
        if (temp.next != null) {
            temp.next.pre = node;
        }
        temp.next = node;
    }

    // 按编号修改
    public void update(HeroNode2 node) {
        HeroNode2 temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (node.no > temp.next.no) {
                temp = temp.next;
            } else if (node.no == temp.next.no) {
                flag = true;
                break;
            } else {
                break;
            }
        }
        if (!flag) {
            System.out.println("要修改的节点不存在！");
        } else {
            temp.next.name = node.name;
            temp.next.nickname = node.nickname;
        }
    }

    // 按编号删除
    public void delete(int no) {
        HeroNode2 temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (no > temp.next.no) {
                temp = temp.next;
            } else if (no == temp.next.no) {
                flag = true;
                break;
            } else {
                break;
            }
        }
        if (!flag) {
            System.out.println("要删除的节点不存在！");
        } else {
            temp.next = temp.next.next;
            if (temp.next != null) {
                temp.next.pre = temp;
            }
        }
    }

    // 显示链表
    public void list() {
        HeroNode2 temp = head;
        while (temp.next != null) {
            System.out.println("no=" + temp.next.no + ",name=" + temp.next.name + ",nickName=" + temp.next.nickname);
            temp = temp.next;
        }
    }
}
class HeroNode2 {
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 pre;
    public HeroNode2 next;

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
